package com.hns.phone.safety.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.domain.ReturnResult;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.frame.mybatis.Pagination;
import com.hns.phone.common.vo.PhoneParamsVo;
import com.hns.phone.safety.service.PhoneMaintFaultsService;
import com.hns.phone.safety.vo.DriverUnsafeRankingVo;
import com.hns.phone.safety.vo.FaultSummeryVo;
import com.hns.phone.safety.vo.FaultsDetail4Phone;
import com.hns.phone.safety.vo.RealTimeFailureParamVo;
import com.hns.phone.safety.vo.UnsafeBhvSummeryVo;
import com.hns.phone.safety.vo.VehicleFaultsRankingVo;
import com.hns.tool.pub.PubUtil;

@Controller
@RequestMapping("/api/phoneMaintFaults")
public class PhoneMaintFaultsController extends BaseController {
	
	@Autowired
    protected CommonInitParamService commonInitParamService;
	
	@Autowired
	private PhoneMaintFaultsService phoneMaintFaultsService;
	
	/**
     * @param entity
     * @param request
     * @param response
     * @throws Exception
     * @author:zhangy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2015年3月16日
     * @功能说明：机务实时故障报表
     */
    @RequestMapping(value="/getMaintenanceRTFaultsForMobile")
    @ResponseBody
    public Object getMaintenanceRTFaultsForMobile(RealTimeFailureParamVo entity,
        HttpServletRequest request) throws Exception {
    	SysUserEntity user = this.getPhoneCurrentUser();
    	ReturnResult reslutVo = new ReturnResult("0", false);
        try{
        	//如果传机构id，则获取用户拥有资源权限
        	commonInitParamService.getResourcePmsByUser(entity,request);
        	//初始化查询参数
        	commonInitParamService.setOrgIds(entity, null, user, Arrays.asList("T_COMM_MFT_ALM"));
        	commonInitParamService.initQueryTableCondition(entity);
        	//初始化开始结束时间
        	commonInitParamService.dateType2DateTime(entity, entity.getTimeType());
        	//DB操作
        	Pagination page = phoneMaintFaultsService.getRealTimeFailure(entity, commonInitParamService);
        	GridPage grid = new GridPage(page);
        	reslutVo.setObj(grid);
        	reslutVo.setSuccess(true);
        	reslutVo.setMsgType("1");
        }catch(Exception e){
        	e.printStackTrace();
        }
        return reslutVo;
    }
    
//    /**
//     * 故障详情
//     * @param entity
//     * @param request
//     * @param response
//     * @throws Exception
//     * @author:zhangyu
//     * @email:zhangyu2222z@sina.cn
//     */
//    @RequestMapping(value="/getFaultsDetailForMobile")
//    @ResponseBody
//    public Object getFaultsDetailForMobile(RealTimeFailureParamVo entity,
//        HttpServletRequest request) throws Exception {
//    	ReturnResult reslutVo = new ReturnResult("0", false);
//    	try{
//    		commonInitParamService.setOrgIds(entity, null, this.getPhoneCurrentUser(), null);
//    		List<FaultsDetail4Phone> faultsDetail = phoneMaintFaultsService.getFaultsDetail4Mobile(entity, request, commonInitParamService);
//    		reslutVo.setSuccess(true);
//        	reslutVo.setMsgType("1");
//    		reslutVo.setObj(faultsDetail);
//    	}catch(Exception e){
//        	e.printStackTrace();
//    	}
//    	return reslutVo;
//    }
    
    /**
     * 故障趋势
     * @param funcType
     * @param dateType
     * @param lineId
     * @param drvSctBhvType
     * @param className
     * @param idxType
     * @return
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/maintFaultTendency")
	@ResponseBody
    public Object maintFaultTendency(String dateType, String carId, String lineId) throws Exception{
		Map<String, Object> map = formatParams(dateType, lineId, carId, null, null);
		List<T>  list= null;
		if(PubUtil.isNotEmpty(carId)){
			list = phoneMaintFaultsService.maintFaultTendency(map, "HarmfulTendencyVo", dateType);
		}else{
			list = phoneMaintFaultsService.maintLineFaultTendency(map, "HarmfulTendencyVo", dateType);
		}
		return this.getReturnResult(list);
	}
    
    
    /**
     * 车辆故障类型排名
     * @param entity
     * @return
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getFaultTypeRanking")
	@ResponseBody
	public Object getFaultTypeRanking(RealTimeFailureParamVo entity){
    	//初始化开始结束时间
    	Pagination page = phoneMaintFaultsService.getFaultTypeRanking(entity);
    	GridPage grid = new GridPage(page);
    	return this.getReturnResult(grid);
	}
    
    /**
     * 故障统计
     * @param entity
     * @return
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getFaultSummery")
	@ResponseBody
    public Object getFaultSummery(PhoneParamsVo entity){
    	entity.setUserid(this.getPhoneCurrentUser().getId());
    	List<FaultSummeryVo> list = phoneMaintFaultsService.getFaultSummery(entity);
    	return this.getReturnResult(list);
    }
    
    /**
     * 车辆故障排名
     * @param entity
     * @return
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getVehicleFaultsRanking")
	@ResponseBody
    public Object getVehicleFaultsRanking(PhoneParamsVo entity){
    	commonInitParamService.dateType2DateTime(entity, entity.getDateType());
    	Pagination page = phoneMaintFaultsService.getVehicleFaultsRanking(entity);
    	GridPage grid = new GridPage(page);
    	return this.getReturnResult(grid);
    }
    
    /**
     * 不安全行为统计
     * @param entity
     * @return
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getUnsafeBhvSummery")
	@ResponseBody
    public Object getUnsafeBhvSummery(PhoneParamsVo entity){
    	entity.setUserid(this.getPhoneCurrentUser().getId());
    	List<UnsafeBhvSummeryVo> list = phoneMaintFaultsService.getUnsafeBhvSummery(entity);
    	return this.getReturnResult(list);
    }
    
//    /**
//     * 不安全行为排名
//     * @param entity
//     * @return
//     * @author:zhangyu
//     * @email:zhangyu2222z@sina.cn
//     */
//    @RequestMapping("/getDriverUnsafeRanking")
//	@ResponseBody
//    public Object getDriverUnsafeRanking(PhoneParamsVo entity){
//    	commonInitParamService.dateType2DateTime(entity, entity.getDateType());
//    	Pagination page = phoneMaintFaultsService.getDriverUnsafeRanking(entity);
//    	GridPage grid = new GridPage(page);
//    	return this.getReturnResult(grid);
//    }
    
    
    /**
	 * 初始化参数
	 * @param dateType 日期类型
	 * @param lineId 线路id
	 * @param drvSctBhvType 不安全行为类型
	 * @return
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	private Map<String, Object> formatParams(String dateType, String lineId, String carId, String drvSctBhvType, String idxType){
		SysUserEntity user = this.getPhoneCurrentUser();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dateType", dateType);
		map.put("lineId", lineId);
		map.put("carId", carId);
		map.put("corpId", user.getCorpId());
		map.put("drvSctBhvType", drvSctBhvType);
		map.put("idxType", idxType);
		map.put("userId", user.getId());
		return map;
	}
    
	
	/**
     * 车辆故障排名（前五）
     * @param entity
     * @return
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getVehicleFaultsRankingForFive")
	@ResponseBody
    public Object getVehicleFaultsRankingForFive(PhoneParamsVo entity){
    	entity.setUserid(this.getPhoneCurrentUser().getId());
    	List<VehicleFaultsRankingVo> list = phoneMaintFaultsService.getVehicleFaultsRankingForFive(entity);
    	return this.getReturnResult(list);
    }
	
    /**
     * 不安全行为排名（前五）
     * @param entity
     * @return
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getDriverUnsafeRankingForFive")
	@ResponseBody
    public Object getDriverUnsafeRankingForFive(PhoneParamsVo entity){
    	entity.setUserid(this.getPhoneCurrentUser().getId());
    	List<DriverUnsafeRankingVo> list = phoneMaintFaultsService.getDriverUnsafeRankingForFive(entity);
    	return this.getReturnResult(list);
    }
	
    
    
}
